package com.example.studytools

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View

private const val TAG = "RulerView"
class RulerView @JvmOverloads constructor(
    private val context: Context?,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) :
    View(context, attrs, defStyleAttr) {
    private var defaultInt = 0

    //间隔，即两条刻度间的距离
     var interval = defaultInt

    //结束值
    private var toValue = defaultInt

    //每两个值之间的间隔数,也指多少个最小单位，比如0cm到1cm有10个最小单位1mm
    var intervalsBetweenValues = defaultInt

    //相邻两个值的跳跃间隔，如上面第一张图的10000到11000，中间的跳跃值就是1000\
    private var valuesInterval = defaultInt

    //值的文本大小
    private var valuesTextSize = defaultInt

    //值的文本颜色
    private var valuesTextColor = defaultInt

    //刻度的宽度
    private var linesWidth = defaultInt

    //刻度的颜色
    private var linesColor = defaultInt

    private val paint = Paint()
//    private val bgPaint = Paint()
    private var textHeight = defaultInt
    private var offSetX = 0f //偏移
    private val offSetY = 10f
    private var minLength = 0f

    init {
        val array = context!!.obtainStyledAttributes(attrs, R.styleable.Ruler)
        interval = array.getDimensionPixelSize(
            R.styleable.Ruler_interval,
            dp2px(intervalsBetweenValues.toFloat()).toInt()
        )
        intervalsBetweenValues =
            array.getInt(R.styleable.Ruler_intervalsBetweenValues, intervalsBetweenValues)
        valuesInterval = array.getInt(R.styleable.Ruler_valuesInterval, 1)
        valuesTextSize =
            array.getDimensionPixelSize(R.styleable.Ruler_valuesTextSize, sp2px(16f).toInt())
        valuesTextColor = array.getColor(R.styleable.Ruler_valuesTextColor, Color.BLACK)
        linesWidth = array.getDimensionPixelSize(R.styleable.Ruler_linesWidth, dp2px(1f).toInt())
        linesColor = array.getColor(R.styleable.Ruler_linesColor, Color.BLACK)

        array.recycle()

        paint.textSize = valuesTextSize.toFloat()
        paint.color = linesColor
        paint.isAntiAlias = true
//        bgPaint.color = Color.WHITE
//        bgPaint.isAntiAlias = true
        //文本高度
        val fm = paint.fontMetrics
        textHeight = (fm.bottom - fm.top).toInt()
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        //根据宽度去计算toValue
        minLength = height / 8f
        //左右两边各减一个刻度用作间隙
        toValue = width / (interval * intervalsBetweenValues)-2
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        if (canvas != null) {
            //画刻度
            paint.color = linesColor
            paint.strokeWidth = linesWidth.toFloat()
            offSetX =
                (width - (toValue * interval * intervalsBetweenValues)) / 2f
            var position: Int = defaultInt
//            canvas.drawRoundRect(0f, 0f, width.toFloat(), height.toFloat(), 5f, 5f, bgPaint)
            var width = offSetX
            while (true) {
                //=====================画刻度============
                if (position > toValue / valuesInterval * intervalsBetweenValues) break
                //这里需要额外加上以文本“10000”的长度作为偏移量，防止值文本很长的时候，文本还没完全退出边界就消失了
                if (width-offSetX > getWidth() + paint.measureText("10000")) break
                if (position % (intervalsBetweenValues / 2) == 0) {
                    if (position % intervalsBetweenValues == 0) {

                        //===============长线============
                        canvas.drawLine(width, offSetY, width, minLength * 2f, paint)
                        paint.color = valuesTextColor
                        val valueString =
                            (position / intervalsBetweenValues * valuesInterval).toString()
                        canvas.drawText(
                            valueString,
                            width - paint.measureText(valueString) / 2,
                            minLength * 2.5f + textHeight / 2,
                            paint
                        )
                        paint.color = linesColor
                    } else {
                        //============中线==============
                        canvas.drawLine(width, offSetY, width, minLength*1.5f, paint)
                    }
                } else {
                    //============短线===========
                    canvas.drawLine(width, offSetY, width, minLength, paint)
                }
                width += interval
                position++
            }
        }

    }

    private fun sp2px(sp: Float): Float {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, resources.displayMetrics)
    }

    private fun dp2px(dp: Float): Float {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.displayMetrics)
    }
}